'''
Read and collect info from MESH_STATS.LOG
'''

import os
import re

hist_name_regex = re.compile('HISTOGRAM FOR:\s+(.*)')
bin_regex = re.compile('(-?[\d.]+(?:E[-\+]?\d+)?)\s+-->\s?(-?[\d.]+(?:E[-\+]?\d+)?).+?(\d+)\s+CELLS')

def safe_float(str):
    try:
        return float(str)
    except ValueError:
        return 0

def read_mesh_stats(fname):
    with open(fname, 'r') as f:
        return parse_mesh_stats(f)

def parse_mesh_stats(f):

    data = {}
    hist_name = None
    hist_data = None
    for line in f:
        if 'HISTOGRAM' in line:
            hn = hist_name_regex.findall(line)
            if hn:
                hist_name = hn[0]
                hist_data = data.setdefault(hist_name, {})
        elif hist_data is not None:
            bin = bin_regex.findall(line)
            if bin and len(bin[0]) == 3:
                bin = [safe_float(v) for v in bin[0]]
                hist_data.setdefault('from', []).append(bin[0])
                hist_data.setdefault('to', []).append(bin[1])
                hist_data.setdefault('n', []).append(bin[2])


    return data


def test():
    import io

    mesh_stats = io.StringIO(
'''
============================================================================
 RANGE OF W-MOMENTUM Alpha_We_c =  0.0000000E+00   0.1000000E+01
 RANGE OF W-MOMENTUM Alpha_Wn_c =  0.0000000E+00   0.1000000E+01
 RANGE OF W-MOMENTUM Alpha_Wt_c =  0.0000000E+00   0.1000000E+01

 RANGE OF W-MOMENTUM Theta_We   =  0.2171293E-01   0.9782871E+00
 RANGE OF W-MOMENTUM Theta_Wn   =  0.5283017E-01   0.9333358E+00
 RANGE OF W-MOMENTUM Theta_Wt   =  0.5000000E+00   0.9920089E+00

 RANGE OF W-MOMENTUM Theta_W_te =  0.9025591E+00   0.1680681E-33
 RANGE OF W-MOMENTUM Theta_W_tn =  0.1663078E-33   0.9345055E+00

 RANGE OF W-MOMENTUM NOC_W_E    = -0.4000733E+03   0.4772660E+03
 RANGE OF W-MOMENTUM NOC_W_N    = -0.2682874E+03   0.8098670E+03
 RANGE OF W-MOMENTUM NOC_W_T    = -0.8704514E+02   0.7489071E+02

 RANGE OF W-MOMENTUM DELH_W     =  0.2546228E-04   0.9876543E+32
============================================================================
============================================================================
HISTOGRAM FOR: AXY (SCALAR STANDARD CELLS)
MIN =   0.1571E-02
MAX =   0.1571E-02
============================================================================
 0.157E-02 --> 0.157E-02 |#########################|   13928 CELLS OR 100.0%
============================================================================
============================================================================
HISTOGRAM FOR: AXZ (SCALAR STANDARD CELLS)
MIN =   0.1538E-02
MAX =   0.1538E-02
============================================================================
 0.154E-02 --> 0.154E-02 |#########################|   13928 CELLS OR 100.0%
============================================================================
============================================================================
HISTOGRAM FOR: AYZ (SCALAR CUT CELLS)
MIN =   0.0000E+00
MAX =   0.1633E-02
============================================================================
 0.000E+00 --> 0.163E-03 |######                   |    1090 CELLS OR  25.6%
 0.163E-03 --> 0.327E-03 |                         |     124 CELLS OR   2.9%
 0.327E-03 --> 0.490E-03 |#                        |     188 CELLS OR   4.4%
 0.490E-03 --> 0.653E-03 |#                        |     290 CELLS OR   6.8%
 0.653E-03 --> 0.817E-03 |##                       |     352 CELLS OR   8.3%
 0.817E-03 --> 0.980E-03 |                         |     154 CELLS OR   3.6%
 0.980E-03 --> 0.114E-02 |#                        |     240 CELLS OR   5.6%
 0.114E-02 --> 0.131E-02 |                         |     162 CELLS OR   3.8%
 0.131E-02 --> 0.147E-02 |#                        |     297 CELLS OR   7.0%
 0.147E-02 --> 0.163E-02 |########                 |    1365 CELLS OR  32.0%
============================================================================
============================================================================
HISTOGRAM FOR: VOL (SCALAR CUT CELLS)
MIN =   0.6303E-06
MAX =   0.6282E-04
============================================================================
 0.630E-06 --> 0.685E-05 |####                     |     706 CELLS OR  16.6%
 0.685E-05 --> 0.131E-04 |##                       |     361 CELLS OR   8.5%
 0.131E-04 --> 0.193E-04 |#                        |     339 CELLS OR   8.0%
 0.193E-04 --> 0.255E-04 |#                        |     250 CELLS OR   5.9%
 0.255E-04 --> 0.317E-04 |#                        |     201 CELLS OR   4.7%
 0.317E-04 --> 0.379E-04 |#                        |     205 CELLS OR   4.8%
 0.379E-04 --> 0.442E-04 |##                       |     355 CELLS OR   8.3%
 0.442E-04 --> 0.504E-04 |##                       |     485 CELLS OR  11.4%
 0.504E-04 --> 0.566E-04 |###                      |     592 CELLS OR  13.9%
 0.566E-04 --> 0.628E-04 |####                     |     768 CELLS OR  18.0%
============================================================================
============================================================================
HISTOGRAM FOR: Aspect Ratio (SCALAR CUT CELLS)
MIN =   0.1040E+01
MAX =   0.1009E+02
============================================================================
 0.104E+01 --> 0.195E+01 |##################	   |    3178 CELLS OR  74.6%
 0.195E+01 --> 0.285E+01 |###                      |     568 CELLS OR  13.3%
 0.285E+01 --> 0.376E+01 |#                        |     334 CELLS OR   7.8%
 0.376E+01 --> 0.466E+01 |                         |	  74 CELLS OR   1.7%
 0.466E+01 --> 0.557E+01 |                         |	  18 CELLS OR   0.4%
 0.557E+01 --> 0.647E+01 |                         |	   2 CELLS OR   0.0%
 0.647E+01 --> 0.738E+01 |                         |	   2 CELLS OR   0.0%
 0.738E+01 --> 0.828E+01 |                         |	   2 CELLS OR   0.0%
 0.828E+01 --> 0.919E+01 |                         |	   0 CELLS OR   0.0%
 0.919E+01 --> 0.101E+02 |                         |	  84 CELLS OR   2.0%
============================================================================
============================================================================
HISTOGRAM FOR: AXY (U-MOMENTUM STANDARD CELLS)
MIN =   0.1571E-02
MAX =   0.1571E-02
============================================================================
 0.157E-02 --> 0.157E-02 |#########################|   13843 CELLS OR 100.0%
============================================================================
============================================================================
HISTOGRAM FOR: AXZ (U-MOMENTUM STANDARD CELLS)
MIN =   0.1538E-02
MAX =   0.1538E-02
============================================================================
 0.154E-02 --> 0.154E-02 |#########################|   13843 CELLS OR 100.0%
============================================================================
'''
    )

    d = parse_mesh_stats(mesh_stats)
    print(d)

if __name__ == '__main__':
    test()
